package work.kie.haqtag;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import core.util.TransformCsvData;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

import static work.MemberUtil.*;


/**
 * @author Gong.Yang
 */
@Slf4j
public class Demo {
    @SneakyThrows
    public static void main(String[] args) {
        List<Item> list = new CopyOnWriteArrayList<>();
        List<String> notFind = new CopyOnWriteArrayList<>();
        EventLoopGroup eventExecutors = new NioEventLoopGroup(32);

        AtomicInteger count = new AtomicInteger();
        TransformCsvData.peekCsv(Setting.filePath + ".csv", 0, x -> {

            Future<String> future = eventExecutors.submit(() -> getThridCode(x[0], Setting.baseUrl, Setting.token));
            future.addListener(result -> {
                String thridCode = (String) result.get();
                if (StrUtil.isBlank(thridCode)) {
                    notFind.add(x[0]);
                } else {
                    list.add(new Item(x[0], thridCode));
                }

                int number = count.getAndIncrement();
                if (number % 1000 == 0) {
                    log.info("【用户信息】获取第{}个用户", number);
                }
            });
        });
        eventExecutors.shutdownGracefully().sync();
        log.info("【用户信息】获取完毕，notFind size->{}", notFind.size());
        FileUtil.writeString(notFind.toString(), Setting.filePath + "/notFindCustomer.txt", Charset.defaultCharset());
        count.set(0);

        EventLoopGroup eventExecutors2 = new NioEventLoopGroup(32);

        for (Item item : list) {

            Future<String> future = eventExecutors2.submit(() -> {
                JSONArray tags = findHAQTag(getRequest(item), Setting.baseUrl, Setting.token);
                if (CollUtil.isEmpty(tags)) {
                    return "not find";
                }
                JSONObject tag = tags.getJSONObject(0);
                return validateTag(tag);
            });

            future.addListener(result -> {
                String status = (String) result.get();
                item.setStatus(status);
                int number = count.getAndIncrement();
                if (number % 1000 == 0) {
                    log.info("【tag处理】获取第{}个用户", number);
                }
            });
        }
        Future<?> future = eventExecutors2.shutdownGracefully();
        future.sync();

        log.info("【tag处理】处理完毕，开始生成报表");
        EasyExcel.write(Setting.filePath + "/out.xlsx").sheet("sheet1").doWrite(list);
    }

    private static String validateTag(JSONObject tag) {
        String tagValue = tag.getStr("tagValue");
        return StrUtil.isBlank(tagValue) ? "null" : tagValue;
    }

    private static JSONObject getRequest(Item item) {
        return buildHAQTagBody(List.of("OT343465"), item.getThirdCode());
    }
}
